﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblProdCat_IndustryDAL:ImtblProdCat_Industry
    {
        HKEA_CMSEntities model;
        mtblProdCat_Industry indeusty;
        public mtblProdCat_IndustryDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblProdCat_IndustryDAL GetInstance()
        {
            return new mtblProdCat_IndustryDAL();
        }
        public int InsertENT(mtblProdCat_Industry ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblProdCat_Industry.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblProdCat_Industry> GetListByENT(mtblProdCat_Industry ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblProdCat_Industry.Where(a => a.IndustryUID > 0);
            if (ent.IndustryUID > 0)
                result = result.Where(a => a.IndustryUID == ent.IndustryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblProdCat_Industry> GetListByENT(mtblProdCat_Industry ent)
        {
            var result = model.mtblProdCat_Industry.Where(a => a.IndustryUID > 0);
            if (ent.IndustryUID > 0)
                result = result.Where(a => a.IndustryUID == ent.IndustryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblProdCat_Industry ent)
        {
            var result = model.mtblProdCat_Industry.Where(a => a.IndustryUID > 0);
            if (ent.IndustryUID > 0)
                result = result.Where(a => a.IndustryUID == ent.IndustryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblProdCat_Industry ent)
        {
            indeusty = model.mtblProdCat_Industry.Where(a=>a.IndustryUID==ent.IndustryUID).FirstOrDefault();
            if (indeusty != null)
            {
                indeusty.Deleted = ent.Deleted;
                indeusty.LastModifyBy = ent.LastModifyBy;
                indeusty.LastModifyDate = DateTime.Now;
                indeusty.ProductCat_IndUID = ent.IndustryUID;
                indeusty.ProductSubCatUID = ent.ProductSubCatUID;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblProdCat_Industry ent)
        {
            indeusty = model.mtblProdCat_Industry.Where(a => a.IndustryUID == ent.IndustryUID).FirstOrDefault();
            if (indeusty != null)
            {
                model.mtblProdCat_Industry.DeleteObject(indeusty);
            }
            return model.SaveChanges();
        }
    }
}
